package cj.web.marry.controllers;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.BaseController;
import cj.web.marry.dao.VoteDAO;
import cj.web.marry.dao.VoteItemDAO;
import cj.web.marry.dao.VoteResultsDAO;
import cj.web.marry.utils.NetworkUtil;

/**
 * 投票
 * @author Administrator
 *
 */
@Path("vote")
public class VoteController extends BaseController{
	
	protected static final Log logger = LogFactory.getLog(VoteController.class); 
	
	@Autowired
	private VoteDAO voteDAO;
	
	@Autowired
	private VoteItemDAO voteItemDAO;
	
	@Autowired
	private VoteResultsDAO voteResultsDAO;
	
	@Get("showVote")
	public String showVote(Model model){
		List<Map> replis = voteDAO.queryAll();
		model.add("replis",replis);
		return "/views/marry/activity/voting/index.vm";
	}
	
	@Get("showVoteItem")
	public String showVoteItem(Model model,@Param("voteId")String voteId,HttpServletRequest request){
		
		String openid = returnIp(request);
		
		List<Map> vote = voteDAO.queryById(Integer.parseInt(voteId));
		
		List<Map> replis = voteItemDAO.queryByVoteId(Integer.parseInt(voteId));
		model.add("replis",replis);
		model.add("voteId",voteId);
		model.add("voteName",vote.get(0).get("name"));
		List<Map> opVotelis = voteResultsDAO.queryByIdAndIp(Integer.parseInt(voteId), openid);
		if(opVotelis!=null && opVotelis.size()>0){
			//存在
			for(Map rep : replis){
				String voteItemId = rep.get("id").toString();
				for(Map opVoteRep : opVotelis){
					String voteItemIds = opVoteRep.get("voteItemIds").toString();
					if(voteItemId.equals(voteItemIds)){
						rep.put("check", "true");
					}
				}
			}
		}
		
		return "/views/marry/activity/voting/voting.vm";
	}
	
	@Post("opVote")
	public String opVote(@Param("voteId") String voteId,@Param("voteItemIds") String voteItemIds,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		String openid = returnIp(request);
		
		List<Map> repVote = voteDAO.queryById(Integer.parseInt(voteId));
		
		String endDate = repVote.get(0).get("endDate").toString();
		int flag = DateUtils.compareDateWithNow(DateUtils.parse(endDate));//与活动时间比较是否结束
		if(flag<0){
			//活动结束
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_6);//结束
			return "@" + jo.toString();
		}
		
		String startDate = repVote.get(0).get("startDate").toString();
		int flagstartDate = DateUtils.compareDateWithNow(DateUtils.parse(startDate));//与活动时间比较是否结束
		if(flagstartDate>0){
			//未开始
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_8);//未开始
			return "@" + jo.toString();
		}
		
		List<Map> voteItemlis =  voteItemDAO.queryById(Integer.parseInt(voteItemIds));
		if(voteItemlis.size()>0){
			int status = Integer.parseInt(voteItemlis.get(0).get("status").toString());
			if(status!=0){
				jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
				jo.put("returnCode", Constants.returnObj.returnCode.code_1);//重复投票
				return "@"+jo.toString();
			}
		}

		List<Map> replis = voteResultsDAO.queryByIdAndIp(Integer.parseInt(voteId), voteItemIds, openid);
		if(replis.size()>0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
			jo.put("returnCode", Constants.returnObj.returnCode.code_1);//重复投票
			return "@"+jo.toString();
		}
		
		try {
			voteItemDAO.updateCount(Integer.parseInt(voteItemIds));//加数量
			voteResultsDAO.create(Integer.parseInt(voteId), voteItemIds, openid);
			jo.put("voteItemIds", voteItemIds);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 返回IP
	 * @param request
	 * @return
	 */
	private String returnIp(HttpServletRequest request){
		String openid = "";
		try {
			openid = NetworkUtil.getIpAddress(request);
		} catch (IOException e1) {
			UUID uuid = UUID.randomUUID();
			openid = uuid.toString();
		}
		return openid;
	}
	
}